﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using Newtonsoft.Json;

namespace App
{
    public partial class JSPost_Check : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                bool isTrue = false;

                switch (action)
                {
                    case "checkEmail":
                        isTrue = isEmail(Request["name"]);
                        Response.Write(isTrue);
                        Response.End();
                        break;
                    case "checkUserName":
                        isTrue = isUserName(Request["name"]);
                        Response.Write(isTrue);
                        Response.End();
                        break;
                    case "getCity":
                        string res = string.Empty;
                        List<AreaInfo> cs = GetCity();
                        if (cs != null && cs.Count > 0)
                        {
                            res = JsonConvert.SerializeObject(cs);
                        }
                        Response.Write(res);
                        Response.End();
                        break;
                    case "checkOldpPssword":
                        string result = checkOldPassWord(Request["value"]);
                        if (string.IsNullOrEmpty(result))
                        {
                            Response.Write("1");
                            Response.End();
                        }
                        else
                        {
                            Response.Write(result);
                            Response.End();
                        }
                        break;
                }
            }
        }



        private bool isEmail(string p)
        {
            UserProfileInfo userProfile = BLL.BUserProfile.getInstance().GetUserProfileInfo(p);
            if (userProfile != null && userProfile.UserPassportId > 0)
                return false;
            return true;
        }

        private bool isUserName(string p)
        {
            UserPassportInfo user = BLL.BUserPassport.getInstance().GetUserPassportInfo(p);
            if (user != null && user.UserPassportId > 0)
                return false;
            return true;

        }
        private List<AreaInfo> GetCity()
        {
            string proIdStr = Request["provinceid"];
            int proCode = 0;
            if (string.IsNullOrEmpty(proIdStr) || !int.TryParse(proIdStr, out proCode))
            {
                return null;
            }
            List<AreaInfo> citys = BLL.BArea.getInstance().GetAreaByParentId(proCode);


            return citys;
        }
        /// <summary>
        /// 检查旧密码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string checkOldPassWord(string value)
        {
            string errMsg = "";

            if (string.IsNullOrEmpty(value)) errMsg = "";
            else if (AppAuthHelper.CurrentUserInfo.Password != value)
            {
                errMsg = "原密码不正确!";
            }

            return errMsg;
        }
    }
}